<?php
namespace app\common\logic;
class AuthManager{


	public static function createKey($type,$data='',$expires=1800,$prefix='Auth_'){
		//生成auth_key  md5(md5(当前时间戳)+16位随机字符串)
		$auth_key=md5(md5(time()).rand_str(16));
		$data['auth_type']=$type;
		$data['create_time']=time();
		cache($prefix."{$auth_key}",$data,$expires);
		return $auth_key;
	}

	public static function updateKey($auth_key,$time=1200,$expires=1800,$prefix='Auth_'){
		$auth_data=AuthManager::getData($auth_key);
		//检测是否需要续期
		if($auth_data && (time()-$auth_data['create_time'])>$time){
			//续期
			$auth_data['create_time']=time();
			cache($prefix."{$auth_key}",$auth_data,$expires);
		}
		return true;
	}

	public static function clearToken($auth_key,$prefix='Auth_'){
		if($auth_key) cache($prefix.$auth_key,null);
		return true;
	}

	public static function getData($auth_key,$prefix='Auth_'){
		return cache($prefix.$auth_key);
	}
}